/*
 * @Author: cola(sunshiwen)
 * @Date: 2022-04-22 10:05:24
 * @LastEditTime: 2022-07-17 17:50:11
 * @LastEditors: cola(sunshiwen)
 * @Description: 分支管理 controller
 * @FilePath: /maycur-koa/controller/branchManageCtrl.js
 */
const {
    project,
    masterBranchManage,
    hybridBranchManage
} = require('../model/branchManageModel');

module.exports = {
    fetchProjectList: async ctx => {
        const data = await project.findAll();
        ctx.body = data;
    },
    fetchMasterBranchList: async ctx => {
        const { pid } = ctx.reqParams.router;
        const data = await masterBranchManage.findAll({ where: { pid } });
        ctx.body = data;
    },
    addNewMasterBranch: async ctx => {
        const {
            corporation,
            env,
            branch,
            version,
            pid,
            commitId,
            person
        } = ctx.reqParams.body;

        const data = await masterBranchManage.findOne({ where: { pid, version } });
        if (data) {
            throw new Error(`该项目中已存在${version}版本`);
        }

        const list = await masterBranchManage.create({
            corporation,
            env,
            branch,
            version,
            pid,
            commitId,
            person,
        });

        ctx.body = 'SUCCESS';
    },
    updateMasterBranch: async ctx => {
        const {
            version,
            pid,
            commitId,
            person
        } = ctx.reqParams.body;
        await masterBranchManage.update({ commitId, person }, { where: { pid, version } });
        ctx.body = 'SUCCESS';
    },
    fetchHybridBranchList: async ctx => {
        const { pid } = ctx.reqParams.router;
        const data = await hybridBranchManage.findAll({ where: { pid } });
        ctx.body = data;
    },
    addNewHybridBranch: async ctx => {
        const {
            corporation,
            env,
            branch,
            version,
            nextVersion,
            pid,
            commitId,
            remark,
        } = ctx.reqParams.body;

        // const data = await hybridBranchManage.findOne({ where: { branch, pid } });
        // if (data) {
        //     throw new Error('该项目中已存在该分支');
        // }

        const list = await hybridBranchManage.create({
            corporation,
            env,
            branch,
            version,
            nextVersion,
            pid,
            commitId,
            remark,
        });

        ctx.body = list;
    },
    updateHybridBranch: async ctx => {
        const {
            pid,
            branch,
            version,
            nextVersion,
            commitId,
            remark,
            id,
        } = ctx.reqParams.body
        await hybridBranchManage.update({ version, nextVersion: nextVersion, commitId, remark, branch, }, { where: { pid, id } });
        ctx.body = 'SUCCESS';
    },
    deleteHybridBranch: async ctx => {
        const { id } = ctx.reqParams.router;
        const data = await hybridBranchManage.destroy({
            where: {
                id,
            }
        });
        if (data <= 0) {
            throw new Error('删除失败');
        }
        ctx.body = 'SUCCESS';
    }
};